package com.zsj.methordArea;

import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Opcodes;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/6/21 15:16
 * @jvmOptions
 * -XX:MetaspaceSize=10m  (设置元空间大小)
 * -XX:MaxMetaspaceSize=10m (设置最大元空间大小)
 * -XX:-CompressedOops (关闭指针压缩)
 *
 */
public class MetaSpaceOOMTest extends ClassLoader {
    public static void main(String[] args) {
        int j = 0;
        try {
            MetaSpaceOOMTest metaSpaceOOMTest = new MetaSpaceOOMTest();
            for (int i = 0; i < 10000; i++) {
                ClassWriter cw = new ClassWriter(0);
                cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Class" + i,
                        null, "java/lang/Object", null);
                byte[] byteArray = cw.toByteArray();
                metaSpaceOOMTest.defineClass("Class" + i, byteArray, 0, byteArray.length);
                j++;
            }
        } finally {
            System.out.println(j);
        }

    }
}
